import React from 'react';
import { Button, message, Popconfirm, Input } from 'antd';
import Modal from '../../../components/Modal/index';
import CommonList from '../../../components/CommonLists/list';
import Publish from './publish';
import services from 'src/services/api';
import Release from './Version/release';
const { TextArea } = Input;
class PolicyList extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'strategyFlow';
    this.operatorWidth = 240;
    this.state = {
      events: [],
      remark: '',
    };
    this.tableParams = {
      scroll: {
        x: 1400,
      },
    };
    this.publishButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.status === 0}
          onClick={record.status === 0 ? this.openPublish.bind(this, record) : this.openRelease.bind(this, record)}
        >
          发布
        </Button>
      );
    };
    this.viewsButton = (record) => {
      return (
        <Button size='small' type='link'>
          详情
        </Button>
      );
    };
    this.laboratoryButton = (record) => {
      return (
        <Button size='small' type='link'>
          实验室
        </Button>
      );
    };
    this.editButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          onClick={this.edit.bind(this, record)}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          编辑
        </Button>
      );
    };
    this.delButton = (record) => {
      return (
        <Popconfirm title='你确定要删除吗？' onConfirm={this.delete.bind(this, record)}>
          <Button size='small' type='link' disabled={record.audit_status === this.AUDIT._WAIT}>
            删除
          </Button>
        </Popconfirm>
      );
    };
    this.copyButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.audit_status === this.AUDIT._WAIT}
          onClick={this.copy.bind(this, record)}
        >
          复制
        </Button>
      );
    };

    this.navButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.audit_status === this.AUDIT._WAIT}
          onClick={this.openVersion.bind(this, record.strategy_id)}
        >
          版本管理
        </Button>
      );
    };
    this.toTemplateButton = (record) => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.audit_status === this.AUDIT._WAIT}
          onClick={() => {
            this.props.toTemplate(record);
          }}
        >
          转为模板
        </Button>
      );
    };
    this.btnGroup = [
      this.editButton,
      this.viewsButton,
      this.laboratoryButton,
      this.delButton,
      this.copyButton,
      this.publishButton,
      this.navButton,
    ];
    this.publishRef = null;
  }

  componentDidMount() {}

  // 删除时的参数
  getDeleteParam = (record) => {
    return { id: record.id };
  };

  // 打开新版本
  openVersion = (id) => {
    this.props.history.push(`/strategy/config/${id}/version`);
  };
  // 策略发布
  openPublish = (record) => {
    Modal.open({
      title: '提示',
      width: '380px',
      okTxt: '发布最新',
      cancelTxt: '查看草稿',
      content: <div>当前策略存在草稿，是否忽略，继续发布最新版本？</div>,
      ok: () => {
        this.openRelease.bind(this, record);
      },
      cancel: () => {},
    });
  };
  // 策略发布
  openRelease = (record) => {
    Modal.open({
      title: '提交发布',
      width: '600px',
      okTxt: '确定',
      isCancelBtn: false,
      isOkBtn: false,
      content: <Release record={record} />,
    });
  };
  delete(record) {
    services[this.api].delete(this.getDeleteParam(record)).then((data) => {
      if (data.code === '0') {
        message.success(data.msg);
        this.props.deleteCb(record);
        this.props.queryList();
      } else {
        message.error(data.msg);
      }
    });
  }
}

export default PolicyList;
